package com.farm.area.controller;

import com.farm.area.pojo.FarmLand;
import com.farm.area.service.FarmLandService;
import com.farm.constant.MessageConstant;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 农田controller
 * @author azhe
 * @create 2020/10/11
 */
@RestController
@CrossOrigin
@RequestMapping("/farmLand")
public class FarmLandController {

    @Autowired
    private FarmLandService farmLandService;

    /**
     * 添加农田，并选择农场片区
     * @param farmLand
     * @param farmId
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody FarmLand farmLand, Integer farmId){
        if(farmLand != null && farmId != null){
            boolean flag = farmLandService.add(farmLand,farmId);
            if(flag){
                return new Result(true, StatusCode.OK,MessageConstant.ADD_FARMlAND_SUCCESS);
            }else {
                return new Result(false,StatusCode.ERROR,MessageConstant.ADD_FARMlAND_FAIL);
            }
        }
        return new Result(false,StatusCode.ERROR,MessageConstant.MESSAGE_IS_NOLL);
    }

    /**
     * 查询所有农田的详细信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Map<String,Object>> farmLandList = farmLandService.findAll();
            return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARMlAND_SUCCESS,farmLandList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.SELECT_FARMlAND_FAIL);
        }
    }

    /**
     * 农田分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public PageResult findPage(@RequestParam Map<String, Object> searchMap, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return farmLandService.findPage(searchMap,page,size);
    }

    /**
     * 根据大田id进行修改
     * @param farmLand
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody FarmLand farmLand){
        int count = farmLandService.updateById(farmLand);
        if(count == 0){
            return new Result(false,StatusCode.ERROR,MessageConstant.UPDATE_FARMlAND_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.UPDATE_FARMlAND_SUCCESS);
    }

    /**
     * 种植农作物
     * @param farmlandId
     * @param cropsId
     * @return
     */
    @PostMapping("/growCrops")
    public Result growCrops(@RequestParam("farmlandId") Integer farmlandId, @RequestParam("cropsId") Integer cropsId){
        int count = farmLandService.growCrops(farmlandId,cropsId);
        if(count == 0){
            return new Result(false,StatusCode.ERROR,MessageConstant.GROW_CROPS_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.GROW_CROPS_SUCCESS);
    }

    /**
     * 安装设备
     * @param farmlandId
     * @param cdid
     * @param position
     * @return
     */
    @PostMapping("/installDevice/{farmlandId}/{cdid}")
    public Result installDevice(@PathVariable("farmlandId") Integer farmlandId, @PathVariable("cdid") Integer cdid, @RequestParam("position") String position){
        int count = farmLandService.installDevice(farmlandId,cdid,position);
        if(count == 0){
            return new Result(false,StatusCode.ERROR, MessageConstant.INSTALL_DEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.INSTALL_DEVICE_SUCCESS);
    }
}
